<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>在线聊天室</title>
    <link type="text/css" href="http://apps.bdimg.com/libs/fontawesome/4.4.0/css/font-awesome.css">
    <style>
        *, *:before, *:after {
            box-sizing: border-box;
        }
        body, html {
            height: 100%;
            overflow: hidden;
        }
        body, ul {
            margin: 0;
            padding: 0;
        }
        body {
            color: #4d4d4d;
            font: 14px/1.4em 'Helvetica Neue', Helvetica, 'Microsoft Yahei', Arial, sans-serif;
            background: #f5f5f5 url('/asset/image/bg.jpg') no-repeat center;
            background-size: cover;
            font-smoothing: antialiased;
        }
        ul {
            list-style: none;
        }
        #chat {
            margin: 20px auto;
            width: 800px;
            height: 600px;
        }
    </style>
</head>
<body>
<div id="chat"></div>
    <c:if test="${applicationScope.accounts!=null}">
        <%--<c:forEach items="${applicationScope.accounts}" var="account">
            <tr>
                <td>${account.value.id}</td>
                <td>${account.value.username}</td>
                &lt;%&ndash;<td>${account.value.password}</td>&ndash;%&gt;
                <td>${account.value.gender}</td>
                <td>${account.value.classes}</td>
                <td>${account.value.school}</td>
            </tr>
        </c:forEach>--%>
    </c:if>
<%--<form id="messageForm">
    来自：<input name="from" type="text"><br>
    <input name="method" type="hidden" value="send">
    发给：<input name="to" type="text"><br>
    内容：<input name="content" type="text"><br>
    <button type="button" onclick="send()">发送</button>
</form>--%>
<h1>显示区</h1>
<div id="showDiv">

</div>
</body>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script src="http://apps.bdimg.com/libs/vue/1.0.8/vue.min.js" type="text/javascript"></script>
<script src="/asset/src/main.js"></script>
<script type="text/javascript">
    var websocket = null;
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        url = "ws://" + window.location.host + "/websocket";
        websocket = new WebSocket(url);
    } else {
        alert('你的浏览器不支持 websocket 的，还是别用ie了')
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        setMessageInnerHTML("WebSocket连接发生错误");
    };

    //连接成功建立的回调方法
    websocket.onopen = function () {
        setMessageInnerHTML("WebSocket连接成功");
    };


    //连接关闭的回调方法
    websocket.onclose = function () {
        setMessageInnerHTML("WebSocket连接关闭");
    };


    //关闭WebSocket连接
    function closeWebSocket() {
        websocket.close();
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
        $.ajax({url:"/closeSession.jsp"});
    };


    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
        $('#showDiv').append("<p>"+innerHTML+"</p>");
    }


    //发送消息
    function send() {
        websocket.send(JSON.stringify($('#messageForm').serializeObject()));
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        setMessageInnerHTML(event.data);
    };

    (function($) {
        $.fn.extend({
            serializeObject : function() {
                var o = {};
                var a = this.serializeArray();
                $.each(a, function() {
                    if (o[this.name]) {
                        if (!o[this.name].push) {
                            o[this.name] = [ o[this.name] ];
                        }
                        o[this.name].push(this.value || '');
                    } else {
                        o[this.name] = this.value || '';
                    }
                });
                return o;
            }
        });

    })(jQuery)
</script>
</html>